<!DOCTYPE html>
<html lang="zh" xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <link rel="stylesheet" href="/layui/css/layui.css" media="all">
  <style>
    .layui-upload-list {
      height: 100px;
      width: 100px;
    }

    .layui-upload-img {
      height: 100px;
      width: 100px;
    }
  </style>
</head>
<body>
<form class="layui-form" lay-filter="menu">
  <div id="id" class="layui-form-item">
    <label class="layui-form-label">id</label>
    <div class="layui-input-inline">
      <input readonly type="text" name="id" autocomplete="off" class="layui-input">
    </div>
  </div>


  <div class="layui-form-item">
    <label class="layui-form-label">门牌号</label>
    <div class="layui-input-inline">
      <input type="text" name="roomNumber" placeholder="请输入用户名" autocomplete="off" class="layui-input">
    </div>
  </div>


  <div class="layui-form-item">
    <label class="layui-form-label">图片</label>
    <input class="layui-hide" type="text" name="imagePath" id="imagePath">
    <div class="layui-input-inline">
      <div class="layui-upload">
        <button type="button" class="layui-btn" id="test1">上传图片</button>
        <div class="layui-upload-list">
          <img class="layui-upload-img" id="demo1">
          <p id="demoText"></p>
        </div>
        <div style="width: 95px;">
          <div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="demo">
            <div class="layui-progress-bar" lay-percent=""></div>
          </div>
        </div>
      </div>
    </div>
  </div>


  <div class="layui-form-item">
    <label class="layui-form-label">座位数</label>
    <div class="layui-input-inline">
      <input type="text" name="seating" placeholder="请输入座位数" autocomplete="off" class="layui-input">
    </div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">楼层</label>
    <div class="layui-input-inline">
      <input name="floor" lay-verify="required" lay-reqtext="楼层不能为空" placeholder="请输入楼层"
             autocomplete="off" class="layui-input">
    </div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">面积</label>
    <div class="layui-input-inline">
      <input type="text" name="area" placeholder="请输入面积" autocomplete="off" class="layui-input">
    </div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">所需分数</label>
    <div class="layui-input-inline">
      <input type="text" name="needScore" placeholder="请输入所需分数" lay-verify="required" autocomplete="off"
             class="layui-input">
    </div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">详情</label>
    <div class="layui-input-inline">
      <textarea name="detail" placeholder="请输入详情" class="layui-textarea"></textarea>
    </div>
  </div>

  <div class="layui-form-item">
    <div class="layui-input-block">
      <button id="insert" class="layui-btn" lay-submit lay-filter="insert">立即提交</button>
      <button id="update" class="layui-btn" lay-submit lay-filter="update">立即提交</button>
      <button id="reset" type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>
<script src="/layui/layui.js" charset="utf-8"></script>
<script src="/jquery/jquery-3.5.1.min.js"></script>
<script>
  // 先初始化
  layui.use(['form', 'layer', 'upload', 'element'], function () {
    const form = layui.form;
    const layer = layui.layer;
    const upload = layui.upload;
    const element = layui.element;

    //常规使用 - 普通图片上传
    const uploadInst = upload.render({
      elem: '#test1'
      , url: '/api/oss/upload' //改成您自己的上传接口
      , before: function (obj) {
        //预读本地文件示例，不支持ie8
        obj.preview(function (index, file, result) {
          $('#demo1').attr('src', result); //图片链接（base64）
        });

        element.progress('demo', '0%'); //进度条复位
        layer.msg('上传中', {icon: 16, time: 0});
      }
      , done: function (res) {
        //如果上传失败
        if (res.code !== 2000) {
          this.error();
          element.progress('demo', '0%'); //进度条复位
          layer.msg('上传失败');
        } else {
          $("#imagePath").val(res.data)
          console.log(res.data)
          layer.msg('上传成功')
          $('#demoText').html(''); //置空上传失败的状态
        }
      }
      , error: function () {
        //演示失败状态，并实现重传
        const demoText = $('#demoText');
        demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
        demoText.find('.demo-reload').on('click', function () {
          uploadInst.upload();
        });
      }
      //进度条
      , progress: function (n, elem, e) {
        element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
        if (n == 100) {
          layer.msg('上传完毕', {icon: 1});
        }
      }
    });

    // 新增
    form.on('submit(insert)', function (data) {
      const params = JSON.stringify(data.field);
      $.ajax({
        url: "/api/room",
        method: 'post',
        contentType: 'application/json',
        data: params,
        dataType: 'json',
        success: function (data) {
          if (data.code === 2000) {
            layer.msg('添加成功')
            setTimeout(() => {
              const index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
              parent.layer.close(index); //再执行关闭
              window.parent.layui.table.reload('roomTable', {}, true);
            }, 1000)
          } else if (data.code === 1000) {
            layer.msg(data.message);
          }
        }
      });
      return false;
    });
    // 修改
    form.on('submit(update)', function (data) {
      const params = JSON.stringify(data.field);
      const id = data.field.id;
      $.ajax({
        url: "/api/room/" + id,
        method: 'put',
        contentType: 'application/json',
        data: params,
        dataType: 'json',
        success: function (data) {
          if (data.code === 2000) {
            layer.msg('修改成功')
            setTimeout(() => {
              const index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
              parent.layer.close(index); //再执行关闭
              window.parent.layui.table.reload('roomTable', {}, true);
            }, 1000)
          } else if (data.code === 1000) {
            layer.msg(data.message);
          }
        }
      });
      return false;
    })
  });

  // 最后赋值
  function formData(data) {
    layui.use(['form', 'layer'], function () {
      const form = layui.form;
      form.val("menu", JSON.parse(JSON.stringify(data)));
    });
  }
</script>
</body>
</html>